<template>
	<view class="content">
		<scroll-view scroll-y class="DrawerPage" :class="modalName=='viewModal'?'show':''">
			<view class="cu-bar bg-white search">
				<view class="search-form round">
					<text class="cuIcon-search"></text>
					<input type="text" v-model="searchValue" placeholder="输入搜索的关键词" confirm-type="search" @confirm="getProductList"></input>
				</view>
				<view class="action">
					<button class="cu-btn bg-gradual-blue shadow-blur round" @click="getProductList">搜索</button>
				</view>
			</view>

			<!-- #ifdef H5 -->
			<view class="bg-white" style="padding-bottom: 200rpx;">
			<!-- #endif -->
				<!-- #ifdef MP-WEIXIN -->
				<view class="bg-white">
				<!-- #endif -->
					<view class="grid margin-bottom text-center col-2">
						<view class="padding solid-bottom" :class="[{'solid-right': (index + 1) % 2 !== 0}]"
							v-for="(item,index) in dataList" :key="index" @click="goToInfo(item)">
							<view class="padding-sm productImage">
								<image :src="item.proImg" mode="widthFix" />
							</view>

							<view class="text-ellipsis" style="width: 100%;">
								{{item.proName}}
							</view>
						</view>
					</view>


					<!-- 暂无数据 -->
					<view v-if="dataList.length === 0" class="text-center margin-top padding-bottom">
						暂无产品信息
					</view>
				</view>


				<view class="flBtn">
					<button class="cu-btn round bg-cyan" data-target="viewModal" @tap="showModal">产品分类</button>
				</view>
		</scroll-view>
		<view class="DrawerClose" :class="modalName=='viewModal'?'show':''" @tap="hideModal">
			<text class="cuIcon-pullright"></text>
		</view>
		<scroll-view scroll-y class="DrawerWindow" :class="modalName=='viewModal'?'show':''">
			<view class="cu-list menu card-menu margin-top-xl margin-bottom-xl shadow-lg">
				<view class="cu-item arrow" v-for="(item,index) in typeList" :key="index">
					<view class="content" @click="selectType(item)">
						<text class="text-grey">{{item.name}}</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 搜索内容
				searchValue: "",
				eq_type: "",

				// 产品列表
				dataList: [],

				modalName: null,

				// 产品分类列表
				typeList: [],
			};
		},
		onShow() {
			const vm = this;

			// 获取产品列表
			vm.getProductList();

			// 获取产品分类列表
			vm.getProTypeList();
		},
		onPullDownRefresh() {
			const vm = this;
			// 清空输入内容
			vm.searchValue = ''
			vm.eq_type = ''
			// 获取项目下设备列表
			vm.getProductList();
		},
		methods: {
			// 获取产品列表
			getProductList() {
				const vm = this;
				var data = {
					"like_proName": vm.searchValue,
					"eq_type": vm.eq_type
				}
				vm.$api.getProductList(data).then(res => {
					console.log(res)
					vm.dataList = res;
				})
			},


			// 获取产品分类列表
			async getProTypeList() {
				try {
					const res = await this.$api.getProTypeList();
					console.log(res);

					const newTypeList = [{
							"code": "",
							"name": "全部产品"
						},
						...res
					];

					this.typeList = newTypeList;
				} catch (error) {
					// 处理错误
					console.error("获取产品类型列表出错:", error);
				}
			},

			// 选择产品类型
			selectType(item) {
				const vm = this;
				vm.eq_type = item.code;
				console.log(item)

				vm.hideModal();
				vm.getProductList();
			},

			showModal(e) {
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			tabSelect(e) {
				this.TabCur = e.currentTarget.dataset.id;
				this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			},

			goToInfo(item) {
				const vm = this;
				uni.navigateTo({
					url: "/pages/product/info" + `?id=${item.id}`
				})
			}
		},
	}
</script>

<style lang="less" scoped>
	.content {
		padding: 0;
	}


	.productImage {
		width: 300rpx;
		height: 350rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.flBtn {
		position: fixed;
		z-index: 99;
		bottom: 150rpx;
		right: 30rpx;
	}

	page {
		background-image: var(--gradualBlue);
		width: 100vw;
		overflow: hidden;
	}

	.DrawerPage {
		position: fixed;
		width: 100vw;
		height: 100vh;
		left: 0vw;
		background-color: #f1f1f1;
		transition: all 0.4s;
	}

	.DrawerPage.show {
		transform: scale(0.9, 0.9);
		left: 85vw;
		box-shadow: 0 0 60upx rgba(0, 0, 0, 0.2);
		transform-origin: 0;
	}

	.DrawerWindow {
		position: absolute;
		width: 85vw;
		height: 100vh;
		left: 0;
		top: 0;
		transform: scale(0.9, 0.9) translateX(-100%);
		opacity: 0;
		pointer-events: none;
		transition: all 0.4s;
		padding: 100upx 0;
	}

	.DrawerWindow.show {
		transform: scale(1, 1) translateX(0%);
		opacity: 1;
		pointer-events: all;
	}

	.DrawerClose {
		position: absolute;
		width: 40vw;
		height: 100vh;
		right: 0;
		top: 0;
		color: transparent;
		padding-bottom: 30upx;
		display: flex;
		align-items: flex-end;
		justify-content: center;
		background-image: linear-gradient(90deg, rgba(0, 0, 0, 0.01), rgba(0, 0, 0, 0.6));
		letter-spacing: 5px;
		font-size: 50upx;
		opacity: 0;
		pointer-events: none;
		transition: all 0.4s;
	}

	.DrawerClose.show {
		opacity: 1;
		pointer-events: all;
		width: 15vw;
		color: #fff;
	}

	.DrawerPage .cu-bar.tabbar .action button.cuIcon {
		width: 64upx;
		height: 64upx;
		line-height: 64upx;
		margin: 0;
		display: inline-block;
	}

	.DrawerPage .cu-bar.tabbar .action .cu-avatar {
		margin: 0;
	}

	.DrawerPage .nav {
		flex: 1;
	}

	.DrawerPage .nav .cu-item.cur {
		border-bottom: 0;
		position: relative;
	}

	.DrawerPage .nav .cu-item.cur::after {
		content: "";
		width: 10upx;
		height: 10upx;
		background-color: currentColor;
		position: absolute;
		bottom: 10upx;
		border-radius: 10upx;
		left: 0;
		right: 0;
		margin: auto;
	}

	.DrawerPage .cu-bar.tabbar .action {
		flex: initial;
	}
</style>